using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShop.Business;
using System.Collections.Generic;
using eShop.Utility;
using eShop.Model;

public partial class myaccount_shopping_list : SitePublicBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            LoadData();
        }
        
    }   

    protected void LoadData()
    {
        int userId = SessionManager.IsUserLoggedIn ? SessionManager.LoggedUser.Id : 0;
        if (userId > 0)
        {
            List<Favorites> favorites = Favorites.GetFavoritesByUserId(userId);
            if (favorites.Count > 0)
            {
                rptFavorites.DataSource = favorites;
                rptFavorites.DataBind();
            }            
        }
    }
    protected void rptFavorites_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            //Favorites favorite = (Favorites)e.Item.DataItem;

            //Image imgProduct = (Image)e.Item.FindControl("imgProduct");
            //imgProduct.ImageUrl = ImageResizer.CheckImageMissing(favorite.Product.ProductImageSmallThumbnail, eShop.Business.SiteSettings.NoImageSmallThumbnailUrl);
        }
    }


    protected void btnItemRemove_Click(object sender, EventArgs e)
    {
        try
        {
            int userId = SessionManager.IsUserLoggedIn ? SessionManager.LoggedUser.Id : 0;

            List<Favorites> favorites = new List<Favorites>();
            foreach (RepeaterItem row in rptFavorites.Items)
            {
                if (row.ItemType == ListItemType.Item || row.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBox cbxSelect = (CheckBox)row.FindControl("cbxSelect");
                    if (cbxSelect != null && cbxSelect.Checked)
                    {
                        HiddenField hdfFavoriteId = (HiddenField)row.FindControl("hdfFavoriteId");
                        int favoriteId;
                        if (int.TryParse(hdfFavoriteId.Value, out favoriteId))
                        {
                            favorites.Add(Favorites.Load(favoriteId));
                        }
                    }
                }
            }
            Favorites.DeleteFavorites(favorites);
            LoadData();
        }
        catch (Exception ex)
        {
            uccResponseMessage.SetFailMessage(ApplicationMessage.CommonFailure);
            uccResponseMessage.Visible = true;
        }
    }

    
    

}
